app.controller('columnCtrl',['$scope','utilService','columnService','$modal','$stateParams','toaster','$state', function($scope,utilService,columnService,$modal,$stateParams,toaster,$state) {

        //查询模块
        $scope.query = function(){
            utilService.queryForPagingList($scope,{
                url:$scope.app.domain + 'column'
            }).then(function(result){
            });
        }

        $scope.query();

        $scope.deleteById = function(id, name){
            var param = {
                id:id,
                name:name
            };
            // query data
            columnService.getTotalModulesCount($scope, id).then(function(result){
                switch (result.status){
                    case 200:
                        if(result.data > 0){
                            param.state=true;
                            openModal("该栏目【" + name + "】正在被其他模块使用,请清空栏目下的模块再删除");
                        }else{
                            param.state=false;
                            openModal("你是否要删除【" + name + "】栏目？");
                        }
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }

            });

            function openModal(msg){
                utilService.modal($modal, $scope, {
                    title:"删除模块",
                    controller:"deleteColumnCtrl",
                    message:msg,
                    data:param
                });
            }

        }
}]);



app.controller('deleteColumnCtrl', ['$scope', '$modalInstance','$state','toaster','modalData','columnService',
    function($scope, $modalInstance,$state,toaster,modalData,columnService) {
        $scope.modal = modalData;
        $scope.column = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if($scope.modal.data.state){
                $modalInstance.dismiss('cancel');
                return;
            }

            if(!$scope.isDelModal){
                return;
            }

            $scope.isDelModal = false;

            columnService.delColumn($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 662:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });


        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }
]);